package com.globalwave.system.web;



import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.globalwave.base.BaseAction;
import com.globalwave.common.ArrayPageList;
import com.globalwave.common.Convertor;
import com.globalwave.common.cache.CodeHelper;
import com.globalwave.system.entity.Dict;
import com.globalwave.system.service.DictService;
import com.globalwave.system.web.annotations.Pid;
import com.opensymphony.xwork2.Preparable;

public class DictAction extends BaseAction implements Preparable {
   
    private static final long serialVersionUID = 1L;

    private DictService dictService = null ;

    private Dict dict ;
    private Dict so ;
    private Long[] dict_ids ;
    private Integer versionId = 0;
    
    public String execute() throws Exception {
        return this.list();    
        
    }

    @Pid(value=131)
    public String list() throws Exception {        
        
        ArrayPageList<Dict> pageList = dictService.query(so, pageIndex, pageSize, null) ;

        renderXML(pageList.asXML()) ;
        
        return null ;    
        
    }
    
    
    public String findOptions() throws Exception { 
    	Map<String,List<Map<String, Object>>> optionsTables = CodeHelper.findOptions(versionId) ;

        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("root") ;

    	for (String tableName:optionsTables.keySet()) {
    		List<Map<String, Object>> rows = optionsTables.get(tableName) ;
    		root.add(Convertor.list2Xml(rows, tableName)) ;
    	}
        log.debug("\n\n" + root.asXML() + "\n\n") ;
        renderXML(root.asXML()) ;
        
    	return null ;
    }

    public String reloadOptions() throws Exception { 
        CodeHelper.reload("Dict") ;
        renderXML("<msg>OK.</msg>") ;
    	return null ;
    }
    
    
    @Pid(value=132)
    public String create()  throws Exception {        
                
        final Object newDict = dictService.create(dict) ;
        CodeHelper.reload("Dict") ;
        
        renderXML(this.getSuccessCreateMessage(newDict).asXML()) ;
        
        return null;    
        
    }
    
    @Pid(value=133)
    public String update()  throws Exception {     
        
        dictService.update(dict) ;
        CodeHelper.reload("Dict") ;

        renderXML(this.getSuccessUpdateMessage(dict).asXML()) ;
        
        return null;    
        
    }
    
    @Pid(value=134)
    public String delete()  throws Exception {
        
        if (dict_ids == null && ids == null) {
            dictService.delete(dict) ;
        } else {
            dictService.deleteAll(dict_ids == null ? ids : dict_ids) ;
        }

        CodeHelper.reload("Dict") ;
        
        renderXML(this.getSuccessDeleteMessage().asXML()) ;
        
        return null;    
        
    }
    
    public void prepare() throws Exception {
        // TODO Auto-generated method stub

    }
        
    // ------------------------------------------------------------------------
    
    // ioc
    public void setDictService(DictService dictService) {
        this.dictService = dictService;
    }

    // entity...
    public Dict getDict() {
        return dict;
    }

    public void setDict(Dict dict) {
        this.dict = dict;
    }

    public void setDict_ids(Long[] dict_ids) {
        this.dict_ids = dict_ids;
    }

    public Dict getSo() {
        return so;
    }

    public void setSo(Dict so) {
        this.so = so;
    }

}
